using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using BCW;
using I2.Loc;
using UnityEngine;

public class RestorePurchases : MonoBehaviour
{
	public static RestorePurchases instance;

	public GameObject loadingTexture;

	public GameObject iCloudPanel_Restored;

	public GameObject iCloudPanel_NotRestored;

	public GameObject iCloudPanel_NoInternet;

	public LoadPurchasesBlockScreen blockScreen;

	private string token = string.Empty;

	private List<string> _productKeysAndroid = new List<string>();

	private void Awake()
	{
		instance = this;
		if (BCWStorage.Get<bool>("androidPurchasedLoadedQW34Yr"))
		{
			SaveAllPurchases();
			BCWStorage.Set("androidPurchasedLoadedQW34Yr", true);
		}
	}

	public void HideRestoredPanel()
	{
		iCloudPanel_Restored.SetActive(false);
	}

	public void HideNotRestoredPanel()
	{
		iCloudPanel_NotRestored.SetActive(false);
	}

	public void HideNoInternetPanel()
	{
		iCloudPanel_NoInternet.SetActive(false);
	}

	private bool isProductPurchased(string productID)
	{
		return _productKeysAndroid.Contains(productID);
	}

	private void RestorePurchase(Product product)
	{
		if (!(product is ProductComsumable))
		{
			MarketController.Instance.BuyProduct(product, true);
		}
	}

	public void RestoreAll(Action OnComplete)
	{
		if (blockScreen != null)
		{
			blockScreen.ShowLoad();
		}
		GpgWrapper.instance.LoadProducts(delegate(string x)
		{
			BCWDebug.LogError(x, "GPG");
			if (x.Contains(","))
			{
				_productKeysAndroid.AddRange(x.Split(','));
				StartCoroutine(LoadAll(OnComplete));
			}
			else if (x.Equals("Authenticate error"))
			{
				if (blockScreen != null)
				{
					blockScreen.ShowEnd(LocalizationManager.GetTermTranslation("Game_Common/not_restored_purchases2"));
				}
				if (OnComplete != null)
				{
					OnComplete();
				}
			}
			else
			{
				if (blockScreen != null)
				{
					blockScreen.ShowEnd(LocalizationManager.GetTermTranslation("Game_Common/not_restored_purchases1"));
				}
				if (OnComplete != null)
				{
					OnComplete();
				}
			}
		});
	}

	private IEnumerator LoadAll(Action OnComplete)
	{
		BCWDebug.Log("Restore All Purchases", "GPG");
		bool restored2 = false;
		foreach (string id in _productKeysAndroid)
		{
			BCWDebug.LogError("Try restore: " + id, "GPG");
			if (MarketController.Instance.allProducts.ContainsKey(id) && !MarketController.Instance.allProducts[id].Purchased)
			{
				restored2 = true;
				RestorePurchase(MarketController.Instance.allProducts[id]);
				yield return null;
			}
		}
		BCWDebug.Log("Try restore Stars", "GPG");
		GpgWrapper.instance.LoadStars();
		yield return null;
		BCWDebug.Log("Try restore account vip", "GPG");
		GpgWrapper.instance.LoadAccountVip();
		yield return null;
		if (blockScreen != null)
		{
			blockScreen.ShowEnd(LocalizationManager.GetTermTranslation("Game_Common/restored_purchases"));
		}
		loadingTexture.SetActive(false);
		if (OnComplete != null)
		{
			BCWDebug.Log("Restore All OnComplete", "GPG");
			OnComplete();
		}
	}

	public void SavePurchase(string productID)
	{
		if (GpgWrapper.instance != null)
		{
			GpgWrapper.instance.AddForFutureSave(productID);
		}
	}

	public void SaveAllPurchases()
	{
		if (GpgWrapper.instance == null)
		{
			return;
		}
		BCWDebug.LogError("SaveAllPurchases", "GPG");
		int num = 0;
		foreach (Product value in MarketController.Instance.allProducts.Values)
		{
			if (value.Purchased)
			{
				GpgWrapper.instance.AddForFutureSave(value.id);
			}
		}
		GpgWrapper.instance.SaveProducts();
	}

	public void DeleteAll()
	{
		GpgWrapper.instance.DeleteProducts();
	}

	private void DeletePurchase(string productID)
	{
	}

	private bool CheckForInternetConnection()
	{
		WebClient webClient = null;
		Stream stream = null;
		try
		{
			webClient = new WebClient();
			stream = webClient.OpenRead("http://www.google.com");
			return true;
		}
		catch (Exception)
		{
			return false;
		}
		finally
		{
			if (webClient != null)
			{
				webClient.Dispose();
			}
			if (stream != null)
			{
				stream.Dispose();
			}
		}
	}
}
